package com.deluxelx.soul.inn.web.course;

import cn.hutool.core.collection.CollectionUtil;
import com.deluxelx.soul.inn.bean.course.CourseDetailVO;
import com.deluxelx.soul.inn.bean.course.CourseVO;
import com.deluxelx.soul.inn.common.param.CollectedParam;
import com.deluxelx.soul.inn.common.result.SoulInnResult;
import com.deluxelx.soul.inn.service.course.CourseService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 课程相关Controller
 *
 * @author liaozihao
 * @date 2024-03-22 16:23
 */
@RestController
@RequestMapping("/soul/inn/course")
public class CourseController {

    @Resource
    private CourseService courseService;

    @GetMapping("/fetchByType")
    public SoulInnResult<List<CourseVO>> fetchCourseListByType (String typeId) {
        List<CourseVO> CourseVOS = courseService.fetchCourseListByType(typeId);
        if (CollectionUtil.isNotEmpty(CourseVOS)) {
            return SoulInnResult.success(CourseVOS);
        }
        return SoulInnResult.fail("根据导航栏类型查询课程列表失败");
    }

    @GetMapping("/courseDetail")
    public SoulInnResult<CourseDetailVO> fetchCourseDetail (String id) {
        CourseDetailVO courseDetailVO = courseService.fetchCourseDetail(id);
        if (courseDetailVO != null) {
            return SoulInnResult.success(courseDetailVO);
        }
        return SoulInnResult.fail("查询课程详情失败");
    }

    @PostMapping("/isCollected")
    public SoulInnResult<Boolean> isCollected(@RequestBody CollectedParam param) {
        try {
            Boolean isCollected = courseService.isCollectCourse(param);
            return SoulInnResult.success(isCollected);
        } catch (Exception e) {
            return SoulInnResult.fail("获取收藏状态失败！");
        }
    }

    @PostMapping("/followCourse")
    public SoulInnResult<Boolean> followCourse(@RequestBody CollectedParam param) {
        try {
            Boolean isSuccess = courseService.followCourse(param);
            return SoulInnResult.success(isSuccess);
        } catch (Exception e) {
            return SoulInnResult.fail("关注失败！");
        }
    }

    @PostMapping("/unFollowCourse")
    public SoulInnResult<Boolean> unFollowCourse(@RequestBody CollectedParam param) {
        try {
            Boolean isSuccess = courseService.unFollowCourse(param);
            return SoulInnResult.success(isSuccess);
        } catch (Exception e) {
            return SoulInnResult.fail("取消关注失败！");
        }
    }

}
